//
//  BagRankListViewController.m
//  HongFuShop
//
//  Created by chirui on 2017/8/8.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "BagRankListViewController.h"
#import "BagRankCollectionViewCell.h"
#import "BagRankDetailsViewController.h" // details

@interface BagRankListViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
{
    
    UICollectionView * _collectionView;
    
    NSMutableArray * _dataArray;
    int _page;
    int _totailPage;
}
@end

@implementation BagRankListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _page = 1;
    
    _dataArray = @[].mutableCopy;

    
    TitleView(self.titleStr);
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_public_return" target:self action:@selector(back)];
    
    [self creatCollectionView];
    [self getActivityListData];
}

-(void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma creatCollectionView

-(void)creatCollectionView {
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:_collectionView];
    _collectionView.showsVerticalScrollIndicator = NO;
    
    MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        _page = 1;
        [self getActivityListData];
        
        
    }];
    _collectionView.mj_header = header;
    
    
    MJRefreshAutoNormalFooter *  footer =[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page++;
        [self getActivityListData];
    }];
    
    footer.automaticallyRefresh = YES;
    _collectionView.mj_footer =footer;
    
    //注册cell类
    [_collectionView registerNib:[UINib nibWithNibName:@"BagRankCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"BagRankCollectionViewCell"];
    
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    
    return _dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary * dic = _dataArray[indexPath.row];
    BagRankCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BagRankCollectionViewCell" forIndexPath:indexPath];
    cell.clipsToBounds = NO;
    [cell.rankImage sd_setImageWithURL:ImageUrl(dic[@"img_src"]) placeholderImage:ImageName(@"img_new_default")];
    cell.rankTitle.text = dic[@"title"];
    
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
     NSDictionary * dic = _dataArray[indexPath.row];
    BagRankDetailsViewController * details =[[BagRankDetailsViewController alloc]init];
    details.dataDic = dic;
    details.typeStr = self.typeStr;
    [self.navigationController pushViewController:details animated:YES];
    
    //    NSDictionary * dataDic = goodsDataArray[indexPath.row];
    //
    //    GoodsDetailsViewController * goods =[[GoodsDetailsViewController alloc]init];
    //    goods.goodsId = [NSString stringWithFormat:@"%@",dataDic[@"gid"]];
    //    [self.navigationController pushViewController:goods animated:YES];
    
    //    EnterViewController * enter =[[EnterViewController alloc]init];
    //    [self.navigationController pushViewController:enter animated:YES];
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    //    第一个参数是宽，第二个参数是高 5s  1.8   6 1.6   6p
//    float height;
//    if (ISP5||ISP4) {
//        height = 1.5;
//    }else if (ISP6){
//        height = 1.3;
//        
//    }else if (ISP6P){
//        height = 1.4;
//    }
//    
    CGSize size=CGSizeMake((WIDTH-10)/2, (WIDTH-10)/2);
    
    return size;
    
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    //上 左 下 右
    return UIEdgeInsetsMake(0, 0, 0,0);
}

//设置水平间隙   默认10
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

////设置垂直间隙  默认10
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    return CGSizeMake(0,10);
}
//设置footer的宽度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    //第一个参数：水平滑动时footer的宽度
    //第二个参数：垂直滑动时footer的高度
    return CGSizeMake(0, 0);
}

-(void)getActivityListData{
    
    
    if (_totailPage != 0) {
        if(_page == _totailPage + 1){
            [_collectionView.mj_footer setState: MJRefreshStateNoMoreData];
            
            return ;
        }
    }
    NSDictionary * dic = @{@"type":self.typeStr,
                           @"page":[NSString stringWithFormat:@"%d",_page],
                           @"perpage":@"10",
                           @"user_id":USER_DEFAULTGet(@"user_id"),
                           @"token":USER_DEFAULTGet(@"token")
                           };
    
    //    SHuiMBhubNoTimeStr(@"正在加载...");
    [PostNetWorking postNetWorkingStr:RequestUrlStr(get_activityList_api) andparameters:dic Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
            _totailPage =  [responseObject[@"datas"][@"total_page"] intValue];
            if (_page==1) {
                
                if (_dataArray) {
                    
                    [_dataArray removeAllObjects];
                }
                if ([responseObject[@"datas"][@"list"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"datas"][@"list"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
            }else {
                
                if ([responseObject[@"datas"][@"list"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"datas"][@"list"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                
                
                if (_dataArray.count>0) {
//                    noBodyView.hidden =YES;
                    if (_dataArray.count<10) {
                        
                        _collectionView.mj_footer.hidden = YES;
                    }
                }else {
                    _collectionView.mj_footer.hidden = YES;
//                    noBodyView.hidden =NO;
//                    
//                    [self.view addSubview:noBodyView];
                }
                [_collectionView.mj_header endRefreshing];
                [_collectionView.mj_footer endRefreshing];
                [_collectionView reloadData];
                
            });
            
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
    } failed:^(NSError *error) {
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"网络错误请重试！");
    }];
    
    
}

@end
